package com.woniu.microcommunityserver.facility.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu.microcommunityserver.activity.entity.ActivityManagementInfoEntity;
import com.woniu.microcommunityserver.facility.entity.CommunalFacilityInfoEntity;
import com.woniu.microcommunityserver.facility.service.CommunalFacilityInfoService;
import com.woniu.microcommunityserver.res.ResponseMsg;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("facility")
public class CommunalFacilityInfoController {
    @Resource
    private CommunalFacilityInfoService communalFacilityInfoService;

    @GetMapping("find")
    public ResponseMsg findAll(int pageNum){
        PageHelper.startPage(pageNum,10);//这行是重点，表示从pageNum页开始，每页pageSize条数据
        List<CommunalFacilityInfoEntity> facilities = communalFacilityInfoService.findAll();
        PageInfo<CommunalFacilityInfoEntity> pageInfo = new PageInfo<CommunalFacilityInfoEntity>(facilities);
        return ResponseMsg.builder().code(200).msg("所有设施查询成功！").data(pageInfo).build();
    }

    @GetMapping("findAny")
    public ResponseMsg findAny(int pageNum,CommunalFacilityInfoEntity communalFacilityInfoEntity){
        System.out.println(communalFacilityInfoEntity);
        PageHelper.startPage(pageNum,10);//这行是重点，表示从pageNum页开始，每页pageSize条数据
        Map<String,Object> map =new HashMap<>();
        if (communalFacilityInfoEntity.getFacilityName()==""){
            communalFacilityInfoEntity.setFacilityName(null);
        }
        if (communalFacilityInfoEntity.getVillageName()==""){
            communalFacilityInfoEntity.setVillageName(null);
        }
        map.put("facilityName",communalFacilityInfoEntity.getFacilityName());
        map.put("villageName",communalFacilityInfoEntity.getVillageName());
        map.put("facilityState",communalFacilityInfoEntity.getFacilityState());
        List<CommunalFacilityInfoEntity> facilities = communalFacilityInfoService.findAny(map);
        PageInfo<CommunalFacilityInfoEntity> pageInfo = new PageInfo<CommunalFacilityInfoEntity>(facilities);
        return ResponseMsg.builder().code(200).msg("指定设施查询成功！").data(pageInfo).build();
    }

    @PostMapping(value = "insert")
    public ResponseMsg insertFacility(@RequestBody CommunalFacilityInfoEntity communalFacilityInfoEntity){
        boolean flag = communalFacilityInfoService.insertFacility(communalFacilityInfoEntity);
        if (flag){
            return ResponseMsg.builder().code(200).msg("新增成功！").build();
        }else {
            return ResponseMsg.builder().code(403).msg("新增失败！").build();
        }
    }

    @DeleteMapping("delete")
    public ResponseMsg deleteFacility(int id){
        boolean flag=communalFacilityInfoService.deleteFacility(id);
        if (flag){
            return ResponseMsg.builder().code(200).msg("删除成功！").build();
        }else {
            return ResponseMsg.builder().code(403).msg("删除失败！").build();
        }
    }

    @PostMapping("update")
    public ResponseMsg updateFacility(@RequestBody CommunalFacilityInfoEntity communalFacilityInfoEntity){
        System.out.println(communalFacilityInfoEntity);
        if(communalFacilityInfoEntity.getFacilityPictureStorageAddress()==""){
            communalFacilityInfoEntity.setFacilityPictureStorageAddress(null);
        }
        boolean flag=communalFacilityInfoService.updateFacility(communalFacilityInfoEntity);
        if (flag){
            return ResponseMsg.builder().code(200).msg("修改成功！").build();
        }else {
            return ResponseMsg.builder().code(403).msg("修改失败！").build();
        }
    }
}
